// 要先导入vue，因为路由vue-router的使用的基础实在vue文件引入基础之上
import Vue from 'vue'
// 引入vue-router文件
import VueRouter from 'vue-router'
// 下面引入文件就是创建的所有路由的模板，相当于template部份，而路由占位符是放在
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/Category/Cate.vue'
import Params from '../components/Category/Params.vue'
import Goods from '../components/Category/Goods.vue'
import Add from '../components/Category/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/Welcome',
    children: [
      { path: '/Welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫，注意它的位置，应该放在暴露之前
router.beforeEach((to, from, next) => {
  // 要去的路径
  if (to.path === '/login') return next()
  const token = sessionStorage.getItem('token')
  if (!token) {
    next('/login')
  } else {
    next()
  }
})
export default router
